<template>
  <table v-if="treeData.familySourceId">
    <tr>
      <td
        :class="{parentLevel: Array.isArray(treeData.children) && treeData.children.length, extend: Array.isArray(treeData.children) && treeData.children.length && treeData.extend}"
        :colspan="Array.isArray(treeData.children) ? treeData.children.length * 2 : 1"
      >
        <div :class="{node: true, hasMate: treeData.mate}">
          <div :class="Array.isArray(treeData.class) ? treeData.class : []"
               class="person"
               @click="$emit('click-node', treeData)"
          >
            <div :class="handleClass(treeData)"/>
            <div class="name">{{ treeData.sourceName }}</div>
          </div>
          <template v-if="Array.isArray(treeData.mate) && treeData.mate.length">
            <div v-for="(mate, mateIndex) in treeData.mate" :key="treeData.sourceName + mateIndex"
                 :class="Array.isArray(mate.class) ? mate.class : []"
                 class="person"
                 @click="$emit('click-node', mate)"
            >
              <div :class="handleClass(mate)"/>
              <div class="name">{{ mate.sourceName }}</div>
            </div>
          </template>
        </div>
        <div v-if="Array.isArray(treeData.children) && treeData.children.length" class="extend_handle"
             @click="toggleExtend(treeData)"
        ></div>
      </td>
    </tr>
    <tr v-if="Array.isArray(treeData.children) && treeData.children.length && treeData.extend">
      <td v-for="(children, index) in treeData.children" :key="index" class="childLevel" colspan="2">
        <TreeChart :json="children" @click-node="$emit('click-node', $event)"/>
      </td>
    </tr>
  </table>
</template>

<script>
export default {
  name: 'TreeChart',
  props: ['json'],
  data() {
    return {
      treeData: {}
    }
  },
  watch: {
    json: {
      handler: function(Props) {
        let extendKey = function(jsonData) {
          jsonData.extend = (jsonData.extend === void 0 ? true : !!jsonData.extend)
          if (Array.isArray(jsonData.children)) {
            jsonData.children.forEach(c => {
              extendKey(c)
            })
          }
          return jsonData
        }
        if (Props) {
          this.treeData = extendKey(Props)
        }
      },
      immediate: true
    }
  },
  methods: {
    handleClass(data) {
      let css = 'avat' // 男
      if (data.sourceSex === '1') {
        css = css + ' circle' // 女
      }
      if (data.sourceCarry === '1') {
        css = css + ' half'
      }
      if (data.sourcePatient === '1') {
        css = css + ' fill'
      }
      if (data.sourceDead === '1') {
        css = css + ' line'
      }
      return css
    },
    toggleExtend(treeData) {
      treeData.extend = !treeData.extend
      this.$forceUpdate()
    }
  }
}
</script>

<style scoped>
table {
  border-collapse: separate !important;
  border-spacing: 0 !important;
}

td {
  position: relative;
  vertical-align: top;
  padding: 0 0 40px 0;
  text-align: center;
}

.extend_handle {
  position: absolute;
  left: 50%;
  bottom: 30px;
  width: 10px;
  height: 10px;
  transform: translate3d(-5px, 0, 0);
  cursor: pointer;
}

.extend_handle:before {
  content: "";
  display: block;
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  border: 2px solid;
  border-color: #ccc #ccc transparent transparent;
  transform: rotateZ(135deg);
  transform-origin: 50% 50% 0;
  transition: transform ease 300ms;
}

.extend_handle:hover:before {
  border-color: #333 #333 transparent transparent;
}

.extend .extend_handle:before {
  transform: rotateZ(-45deg);
}

.extend::after {
  content: "";
  position: absolute;
  left: 50%;
  bottom: 15px;
  height: 15px;
  border-left: 2px solid #ccc;
  transform: translate3d(-1px, 0, 0)
}

.childLevel::before {
  content: "";
  position: absolute;
  left: 50%;
  bottom: 100%;
  height: 15px;
  border-left: 2px solid #ccc;
  transform: translate3d(-1px, 0, 0)
}

.childLevel::after {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  top: -15px;
  border-top: 2px solid #ccc;
}

.childLevel:first-child:before, .childLevel:last-child:before {
  display: none;
}

.childLevel:first-child:after {
  left: 50%;
  height: 15px;
  border: 2px solid;
  border-color: #ccc transparent transparent #ccc;
  border-radius: 6px 0 0 0;
  transform: translate3d(1px, 0, 0)
}

.childLevel:last-child:after {
  right: 50%;
  height: 15px;
  border: 2px solid;
  border-color: #ccc #ccc transparent transparent;
  border-radius: 0 6px 0 0;
  transform: translate3d(-1px, 0, 0)
}

.childLevel:first-child.childLevel:last-child::after {
  left: auto;
  border-radius: 0;
  border-color: transparent #ccc transparent transparent;
  transform: translate3d(1px, 0, 0)
}

.node {
  position: relative;
  display: inline-block;
  margin: 0 1em;
  box-sizing: border-box;
  text-align: center;
}

.node .person {
  position: relative;
  display: inline-block;
  z-index: 2;
  width: 6em;
  overflow: hidden;
}

.node .person .avat {
  display: block;
  width: 3em;
  height: 3em;
  margin: auto;
  overflow: hidden;
  background: #fff;
  border: 1px solid #ccc;
  box-sizing: border-box;
}

.node .person .name {
  height: 2em;
  line-height: 2em;
  overflow: hidden;
  width: 100%;
}

.node.hasMate::after {
  content: "";
  position: absolute;
  left: 4.5em;
  right: 4.5em;
  top: 1.5em;
  border-top: 2px solid #ccc;
  z-index: 1;
}

.node .person .circle {
  border-radius: 50%;
}

.node .person .line {
  position: relative
}

.node .person .line:after {
  position: absolute;
  left: 0;
  top: 0;
  content: "";
  width: 100%;
  height: 100%;
  z-index: 1;
  background: linear-gradient(135deg, transparent 49.5%, deeppink 49.5%, deeppink 50.5%, transparent 50.5%);
}

.node .person .fill {
  background: #999;
}

.node .person .half {
  background: linear-gradient(90deg, #999 50%, #fff 50%);
}
</style>
